<?php

class openapi_ctl_admin_test extends desktop_controller {

    var $workground = "setting_tools";

    public function test() {
        $conf = openapi_conf::getMethods();
        $methods = array_keys($conf);
        foreach ($methods as $v) {
            $methods = get_class_methods('openapi_api_function_v1_' . $v);
            $paramsObj = kernel::single('openapi_api_params_v1_' . $v);
            foreach ($methods as $method) {
                if ($paramsObj->getAppParams($method)) {
                    $list[] = $v . '.' . $method;
                }
            }
        }
        $this->pagedata['list'] = $list;
        $this->page('admin/test/test.html');
    }

    public function ajaxResult() {
        if (! $_POST['apiName'])
            return;
        $info = explode('.', $_POST['apiName']);
        $class = $info[0];
        $function = $info[1];
        
        $obj = kernel::single('openapi_api_params_v1_' . $class);
        $list = $obj->getAppParams($function);
        $description = $obj->description($function);
        
        $this->pagedata['list'] = $list;
        $this->pagedata['post'] = $_POST;
        $this->pagedata['url'] = 'http://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER["SERVER_PORT"] . '/' . $_SERVER['SCRIPT_NAME'] . '/openapi/rpc/service/';
        
        $this->pagedata['description'] = $description;
        $this->display('admin/test/apiForm.html');
    }

    public function result() {
        // $poParams = kernel::single('openapi_api_params_v1_po');
        // $getList = $poParams->getAppParams('getList');
        if ($_POST['url']) {
            $url = $_POST['url'];
        } else {
            $url = 'http://' . $_SERVER['SERVER_NAME'] . '/' . $_SERVER['SCRIPT_NAME'] . '/openapi/rpc/service/';
        }
        unset($_POST['url']);
        $token = $_POST['token'];
        $method = $_POST['method1'];
        unset($_POST['token']);
        unset($_POST['method1']);
        
        $params = $_POST;
        $params['ver'] = 1;
        $params['method'] = $method;
        
        $params['type'] = $_POST['data_format'];
        unset($params['data_format']);
        $params['charset'] = 'utf-8';
        $params['timestamp'] = time();
        // $params['page_no'] = 1;
        // $params['page_size'] = 100;
        $sign = $this->gen_sign($params, $token);
        $params['sign'] = $sign;
        
        $http = kernel::single('openapi_curl');
        $response = $http->set_timeout(300)->post($url, $params, $headers);
        
        if ($params['type'] == 'json') {
            $res['response'] = json_decode($response);
        } else {
            $res['response'] = $response;
        }
        
        $res['request'] = $params;
        // $res['request'] = $params;
        // echo "[request_url]:$url\r\n";
        // echo "[request]:\r\n";
        // foreach ($params as $key => $value) {
        // echo $key . '=' . $value . "\r\n";
        // }
        // print_r($res);exit;
        // echo "\r\n[response]:\r\n" . $response . "\r\n";
        
        echo json_encode($res);
    }

    private function gen_sign($params, $token) {
        if (! $token) {
            return false;
        }
        return strtoupper(md5(strtoupper(md5($this->assemble($params))) . $token));
    }

    private function assemble($params) {
        if (! is_array($params))
            return null;
        ksort($params, SORT_STRING);
        $sign = '';
        foreach ($params as $key => $val) {
            if (is_null($val))
                continue;
            if (is_bool($val))
                $val = ($val) ? 1 : 0;
            $sign .= $key . (is_array($val) ? $this->assemble($val) : $val);
        }
        return $sign;
    }
}